Extract opendirat() helper function into libotutil
authorColin Walters <walters@verbum.org>
Tue, 16 Sep 2014 15:15:36 +0000 (11:15 -0400)
committerColin Walters <walters@verbum.org>
Tue, 16 Sep 2014 15:34:39 +0000 (11:34 -0400)
We were duplicating the code to do an opendirat() in a few places.

Makefile-otutil.am
src/libostree/ostree-repo.c
src/libostree/ostree-sysroot-deploy.c
src/libotutil/ot-fs-utils.c [new file with mode: 0644]
src/libotutil/ot-fs-utils.h [new file with mode: 0644]
src/libotutil/otutil.h

index 8f77543d982781c51d596654ee0b23e37a7654f4..ffd38e09d18190f6239a9a4a89a95882068e402d 100644 (file)
@@ -22,6 +22,8 @@ noinst_LTLIBRARIES += libotutil.la
 libotutil_la_SOURCES = \
        src/libotutil/ot-checksum-utils.c \
        src/libotutil/ot-checksum-utils.h \
+       src/libotutil/ot-fs-utils.c \
+       src/libotutil/ot-fs-utils.h \
        src/libotutil/ot-keyfile-utils.c \
        src/libotutil/ot-keyfile-utils.h \
        src/libotutil/ot-opt-utils.c \
index daa532e714d232f304f0ea5d5837f0bb2028035e..7069d01d6221aef215ffd9cc6f228a0bc7f16e71 100644 (file)
@@ -1100,7 +1100,7 @@ list_loose_objects (OstreeRepo                     *self,
       buf[0] = hexchars[c >> 4];
       buf[1] = hexchars[c & 0xF];
       buf[2] = '\0';
-      dfd = openat (self->objects_dir_fd, buf, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC);
+      dfd = ot_opendirat (self->objects_dir_fd, buf, FALSE);
       if (dfd == -1)
         {
           if (errno == ENOENT)
index ca12a3a9b81e41a1ec0bef08d14f93a60990251d..fbb3ea1d7088a3a67a5f35f07a52522c04d1da9e 100644 (file)
@@ -170,12 +170,8 @@ copy_dir_recurse_fsync (int              src_parent_dfd,
   struct dirent *dent;
   gs_unref_variant GVariant *xattrs = NULL;
 
-  src_dfd = openat (src_parent_dfd, name, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC);
-  if (src_dfd == -1)
-    {
-      ot_util_set_error_from_errno (error, errno);
-      goto out;
-    }
+  if (!ot_gopendirat (src_parent_dfd, name, TRUE, &src_dfd, error))
+    goto out;
 
   /* Create with mode 0700, we'll fchmod/fchown later */
   if (mkdirat (dest_parent_dfd, name, 0700) != 0)
@@ -184,12 +180,8 @@ copy_dir_recurse_fsync (int              src_parent_dfd,
       goto out;
     }
 
-  dest_dfd = openat (dest_parent_dfd, name, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC);
-  if (dest_dfd == -1)
-    {
-      ot_util_set_error_from_errno (error, errno);
-      goto out;
-    }
+  if (!ot_gopendirat (dest_parent_dfd, name, TRUE, &dest_dfd, error))
+    goto out;
 
   /* Clone all xattrs first, so we get the SELinux security context
    * right.  This will allow other users access if they have ACLs, but
@@ -315,7 +307,7 @@ copy_modified_config_file (int                 orig_etc_fd,
   if (parent_slash != NULL)
     {
       parent_path = g_strndup (path, parent_slash - path);
-      dest_parent_dfd = openat (new_etc_fd, parent_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NOCTTY);
+      dest_parent_dfd = ot_opendirat (new_etc_fd, parent_path, FALSE);
       if (dest_parent_dfd == -1)
         {
           if (errno == ENOENT)
diff --git a/src/libotutil/ot-fs-utils.c b/src/libotutil/ot-fs-utils.c
new file mode 100644 (file)
index 0000000..1d02003
--- /dev/null
@@ -0,0 +1,50 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2014 Colin Walters <walters@verbum.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "ot-fs-utils.h"
+
+int
+ot_opendirat (int dfd, const char *path, gboolean follow)
+{
+  int flags = O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY;
+  if (!follow)
+    flags |= O_NOFOLLOW;
+  return openat (dfd, path, flags);
+}
+
+gboolean
+ot_gopendirat (int             dfd,
+               const char     *path,
+               gboolean        follow,
+               int            *out_fd,
+               GError        **error)
+{
+  int ret = ot_opendirat (dfd, path, follow);
+  if (ret == -1)
+    {
+      ot_util_set_error_from_errno (error, errno);
+      return FALSE;
+    }
+  *out_fd = ret;
+  return TRUE;
+}
+
diff --git a/src/libotutil/ot-fs-utils.h b/src/libotutil/ot-fs-utils.h
new file mode 100644 (file)
index 0000000..d7ece4f
--- /dev/null
@@ -0,0 +1,35 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2014 Colin Walters <walters@verbum.org>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#pragma once
+
+#include "ot-unix-utils.h"
+
+G_BEGIN_DECLS
+
+int ot_opendirat (int dfd, const char *path, gboolean follow);
+gboolean ot_gopendirat (int             dfd,
+                        const char     *path,
+                        gboolean        follow,
+                        int            *out_fd,
+                        GError        **error);
+
+G_END_DECLS
+
index e0e30d51a0ceeaa47b384df4ec0b1c7271cb40bb..c78a9c6955f20d5f5686f8a126eb5861b03eb5cb 100644 (file)
@@ -40,6 +40,7 @@
 #include <ot-waitable-queue.h>
 #include <ot-keyfile-utils.h>
 #include <ot-gio-utils.h>
+#include <ot-fs-utils.h>
 #include <ot-opt-utils.h>
 #include <ot-unix-utils.h>
 #include <ot-variant-utils.h>